package cn.cc.baycar.controller.controller;

import cn.cc.baycar.controller.dto.AddPayCarDTO;
import cn.cc.baycar.controller.vo.AddPayCarVO;
import cn.cc.baycar.service.PayCarService;
import cn.cc.baycar.service.bo.AddPayCarDO;
import cn.cc.common.beanCopy.utils.BeanCopierUtil;
import cn.cc.common.result.utils.RestResult;
import cn.cc.common.result.utils.RestResultEmum;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @program: smail-mi-cloud
 * @description: 购物车功能
 * @author: Mr.WuChao
 * @create: 2021-07-21 15:37
 **/
@RestController
@RequestMapping("/payCar")
public class PayCarController {
    @Autowired
    private PayCarService payCarService;


    @PostMapping("/addPayCar")
    public RestResult<Void> addPayCar(AddPayCarDTO dto)  {
        AddPayCarDO payCarDO = BeanCopierUtil.copyObj(dto, AddPayCarDO.class);
        RestResult<Void> result = payCarService.addPayCarByCheck(payCarDO);
        if (result.getCode().equals(result.getCode())){
            return new RestResult<>();
        }
        return new RestResult<>(RestResultEmum.SAVE_ERROR.code,RestResultEmum.SAVE_ERROR.msg);
    }


    @PostMapping("/addPayCarEs")
    public RestResult<Void>  addPayCarEs(AddPayCarDTO dto) throws IOException {
        AddPayCarDO payCarDO = BeanCopierUtil.copyObj(dto, AddPayCarDO.class);
        RestResult<Void> result = payCarService.addPayCarByEs(payCarDO);
        if (result.getCode().equals(result.getCode())){
            return new RestResult<>();
        }
        return new RestResult<>(RestResultEmum.SAVE_ERROR.code,RestResultEmum.SAVE_ERROR.msg);
    }

    @PostMapping("/updPayCar")
    RestResult<Void> updPayCarBy(AddPayCarDTO dto){
        AddPayCarDO payCarDO = BeanCopierUtil.copyObj(dto, AddPayCarDO.class);
        RestResult<Void> result = payCarService.updPayCarByCheck(payCarDO);
        if (result.getCode().equals(result.getCode())){
            return new RestResult<>();
        }
        return new RestResult<>(RestResultEmum.EDIT_ERROR.code,RestResultEmum.EDIT_ERROR.msg);
    }

    @GetMapping("/getPayCar")
    RestResult<List<AddPayCarVO>> getPayCar(){
        List<AddPayCarDO> data = payCarService.getPayCar().getData();
        List<AddPayCarVO> vos = BeanCopierUtil.copyList(data, AddPayCarVO.class);
        if (vos==null||vos.size()==0){
            return new RestResult<>(RestResultEmum.EXIT_ERROR.code,RestResultEmum.EXIT_ERROR.msg);
        }
        return new RestResult<>(vos);
    }
}
